import isWindow from './isWindow';

type Target = HTMLElement | Document | Window;
type Position = {
  left: number;
  top: number;
};

export default function scrollTo(target: Target, position: Position) {
  const { left, top } = position;
  if (isWindow(target)) {
    (target as Window).scrollTo(left, top);
  } else if ((target as Target) instanceof Document) {
    (target as Document).documentElement.scrollLeft = left;
    (target as Document).documentElement.scrollTop = top;
  } else {
    (target as HTMLElement).scrollLeft = left;
    (target as HTMLElement).scrollTop = top;
  }
}
